package com.xmtlxx.oms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import com.xmtlxx.oms.domain.bo.ClassifyBo;
import com.xmtlxx.oms.domain.vo.ClassifyVo;
import com.xmtlxx.oms.service.IClassifyService;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 财物分类Controller
 *
 * @author xmtlxx
 * @date 2022-08-12
 */
@Validated
@Api(value = "财物分类控制器", tags = {"财物分类管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/oms/classify")
public class ClassifyController extends BaseController {

    private final IClassifyService iClassifyService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<ClassifyVo> initBean(){
        ClassifyVo vo = new ClassifyVo();
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询财物分类列表
     */
    @ApiOperation("查询财物分类列表")
    @SaCheckPermission("oms:classify:list")
    @GetMapping("/list")
    public R<List<ClassifyVo>> list(@Validated(QueryGroup.class) ClassifyBo bo) {
        return R.ok(iClassifyService.queryList(bo));
    }

    @ApiOperation("查询财物分类列表")
    @SaCheckPermission("oms:classify:list")
    @GetMapping("/list/exclude/{classifyId}")
    public R<List<ClassifyVo>> excludeChild(@ApiParam("分类ID") @PathVariable(value = "classifyId", required = false) String classifyId) {
        List<ClassifyVo> items = iClassifyService.queryList(new ClassifyBo());
        items.removeIf(c -> c.getId().equals(classifyId) || ArrayUtil.contains(StringUtils.split(c.getAncestors(), ","), classifyId + ""));
        return R.ok(items);
    }

    @ApiOperation("查询财物分类下拉树列表")
    @GetMapping("/treeselect")
    public R<List<Tree<String>>> treeselect() {
        List<ClassifyVo> items = iClassifyService.queryList(new ClassifyBo());
        return R.ok(iClassifyService.buildClassifyTreeSelect(items));
    }

    /**
     * 导出财物分类列表
     */
    @ApiOperation("导出财物分类列表")
    @SaCheckPermission("oms:classify:export")
    @Log(title = "财物分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ClassifyBo bo, HttpServletResponse response) {
        List<ClassifyVo> list = iClassifyService.queryList(bo);
        ExcelUtil.exportExcel(list, "财物分类", ClassifyVo.class, response);
    }

    /**
     * 获取财物分类详细信息
     */
    @ApiOperation("获取财物分类详细信息")
    @SaCheckPermission("oms:classify:query")
    @GetMapping("/{id}")
    public R<ClassifyVo> getInfo(@ApiParam("主键")
                                     @NotNull(message = "主键不能为空")
                                     @PathVariable("id") String id) {
        return R.ok(iClassifyService.queryById(id));
    }

    /**
     * 新增财物分类
     */
    @ApiOperation("新增财物分类")
    @SaCheckPermission("oms:classify:add")
    @Log(title = "财物分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ClassifyVo vo) {
        ClassifyBo bo = new ClassifyBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iClassifyService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改财物分类
     */
    @ApiOperation("修改财物分类")
    @SaCheckPermission("oms:classify:edit")
    @Log(title = "财物分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ClassifyVo vo) {
        ClassifyBo bo = new ClassifyBo();
        BeanUtil.copyProperties(vo,bo);
        return toAjax(iClassifyService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除财物分类
     */
    @ApiOperation("删除财物分类")
    @SaCheckPermission("oms:classify:remove")
    @Log(title = "财物分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iClassifyService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
